import Vue from 'vue';
import Axios from 'axios';
import ApiUrl from '@/constant/ApiUrl';
import CommonUnit from '@/unit/CommonUnit';
import {Route} from 'vue-router';
import store from '@/store/index';
import Http from '@/service/Http';
import {updateUser} from '@/store/dispatches';
import {InterfaceUser} from '@/interface/InterfaceUser';

export default class Scene extends Vue {
    public api = ApiUrl;
    public imageFileDoamin = 'http://wx.chuangyijiaoyu2018.com/';
    public domain = 'http://www.youhuiwl.com/';
    public webPath = this.domain + 'youhuimall.html';
    public cryptoKey: string = 'sgg45747ss223411';

    public constructor() {
        super();

        if (process.env.NODE_ENV === 'development') {
            ApiUrl.url = '/site/';
        } else if (window.location.protocol === 'file:' || window.location.protocol === 'files:') {
            ApiUrl.url = 'http://www.youhuiwl.com';
        }

        // if (window.history && window.history.pushState) {
        //     window.addEventListener('popstate', (e) => {
        //         window.history.pushState('', '', '');
        //     });
        // }
    }

    public navigateTo(obj: any, auth: boolean = true): void {
        const storeState: any = store.state;
        if (storeState.DeviceStore.status && storeState.SettingStore.SoundAndShock.actionShock) {
            navigator.vibrate(200);
        }
        if (auth) {
            this.authentication();
        }
        obj.query = {...obj.query, referer: this.$route.name};
        this.$router.push(obj);
    }

    public switchTab(obj: any) {
        const storeState: any = store.state;
        if (storeState.DeviceStore.status && storeState.SettingStore.SoundAndShock.actionShock) {
            navigator.vibrate(200);
        }
        this.authentication();
        this.$router.replace(obj);
    }

    public navigateBack(n: number = -1, auth: boolean = true): void {
        const storeState: any = store.state;
        if (storeState.DeviceStore.status && storeState.SettingStore.SoundAndShock.actionShock) {
            navigator.vibrate(200);
        }
        if (auth) {
            this.authentication();
        }
        if (this.$route.query.referer && window.history.length > 1) {
            this.$router.go(n);
        } else {
            this.reLaunch();
        }
    }

    public reLaunch(): void {
        this.$router.replace({path: '/'});
    }

    public redirectTo(obj: any = {}, auth: boolean = true): void {
        if (auth) {
            this.authentication();
        }
        if (!obj.name && !obj.path) {
            this.$router.replace({path: '/'});
        } else {
            if (this.$route.query.referer) {
                obj.query = {...obj.query, referer: this.$route.name};
            }
            this.$router.replace(obj);
        }
    }

    public navigateLoginPage(): void {
        this.redirectTo({name: 'loginRegister'}, false);
    }

    public imageUrlHandle(str: string): string {
        return this.imageFileDoamin + String(str);
    }

    public globalSingleResultToast(res: any): boolean {
        (this as any).$createToast({
            mask: true,
            type: 'error',
            time: 2500,
            zIndex: 1000,
            txt: res.msg,
            onTimeout: () => {
                if (!CommonUnit.responseLoginVerification(res)) {
                    this.navigateLoginPage();
                }
            },
        }).show();
        return CommonUnit.responseLoginVerification(res);
    }

    public globalMultipleResultToast(...arg: any): boolean {
        const errorList: string[] = [];
        let loginResult: boolean = true;
        for (const error of arg) {
            if (error.code !== 1) {
                errorList.push(error.msg);
            }
            if (error.code === -1) {
                loginResult = false;
            }
        }
        const virtual: any = {
            msg: errorList.join(','),
            code: loginResult ? 0 : -1,
            data: '',
        };

        (this as any).$createToast({
            mask: true,
            type: 'error',
            time: 2500,
            zIndex: 1000,
            txt: virtual.msg,
            onTimeout: () => {
                if (!CommonUnit.responseLoginVerification(virtual)) {
                    this.navigateLoginPage();
                }
            },
        }).show();
        return CommonUnit.responseLoginVerification(virtual);
    }

    public authentication(): void {
        const storeState: any = store.state;
        if (this.$route.meta.loginAuth&&(storeState.UserStore.token!=='')) {
            Http.get(this.api.getUserInfo, {}, {token: storeState.UserStore.token}).then((res) => {
                if (res.code === 1) {
                    const user: any = {...res.data, ...res.data.user_info};
                    delete user.user_info;
                    updateUser(CommonUnit.objectUnderlineToHump(user));
                } else {
                    this.globalSingleResultToast(res);
                }
            });
        }
    }

    public userFaceSafeHandle(user: any): any {
        const defaultFace: any = {
            extension: 'jpg',
            file_name: 'user-no-face.jpg',
            file_path: user.gender === 2 ? require('@/assets/images/default-user-face-women.png') : require('@/assets/images/default-user-face-mam.png'),
            file_size: 6103,
            file_status: {id: 0, user_id: user.userId, file_id: 0, content: '', type: 1, status: 1},
            file_type: 'image',
            file_url: 'localhost',
            group_id: 2,
            id: 0,
            is_delete: 0,
            is_user: 1,
            storage: 'localhost',
        };
        if (user.avatar_id === 0) {
            return defaultFace;
        } else if (user.avatar.file_status.status !== 1) {
            return defaultFace;
        } else {
            return user.avatar;

        }
    }

    public getWeChatCode(url: string): void {
        if (CommonUnit.wechatBrowser()) {
             window.location.href = 'http://www.youhuiwl.com/api/v1/getCode?url=' + url;
        }
    }

}


